package com.pigdroid.adbsqlite.jdbc;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import org.apache.log4j.Logger;

public class ADBSQLiteResultSetMetaData implements ResultSetMetaData {

	private ADBSQLiteResultSet resultSet;
	private static final Logger LOG = Logger.getLogger(ADBSQLiteResultSetMetaData.class);

	public ADBSQLiteResultSetMetaData(ADBSQLiteResultSet adbsqLiteResultSet) {
		LOG.info("Created new metadata");
		this.resultSet = adbsqLiteResultSet;
	}

	public boolean isWrapperFor(Class<?> arg0) throws SQLException {
		LOG.info("isWrapperFor");
		return false;
	}

	public <T> T unwrap(Class<T> arg0) throws SQLException {
		LOG.info("unwrap");
		return null;
	}

	public String getCatalogName(int arg0) throws SQLException {
		LOG.info("getCatalogName");
		return getSchemaName(arg0);
	}

	public String getColumnClassName(int arg0) throws SQLException {
		LOG.info("getColumnClassName");
		return String.class.getName();
	}

	public int getColumnCount() throws SQLException {
		LOG.info("getColumnCount");
		return resultSet.getIterator().getHeaderCount();
	}

	public int getColumnDisplaySize(int arg0) throws SQLException {
		LOG.info("getColumnDisplaySize");
		return 32;
	}

	public String getColumnLabel(int arg0) throws SQLException {
		LOG.info("getColumnLabel");
		return resultSet.getIterator().getHeader(arg0);
	}

	public String getColumnName(int arg0) throws SQLException {
		LOG.info("getColumnName");
		return resultSet.getIterator().getHeader(arg0);
	}

	public int getColumnType(int arg0) throws SQLException {
		LOG.info("getColumnType");
		return java.sql.Types.VARCHAR;
	}

	public String getColumnTypeName(int arg0) throws SQLException {
		LOG.info("getColumnTypeName");
		return "java.sql.Types.VARCHAR";
	}

	public int getPrecision(int arg0) throws SQLException {
		LOG.info("getPrecision");
		return 0;
	}

	public int getScale(int arg0) throws SQLException {
		LOG.info("getScale");
		return 0;
	}

	public String getSchemaName(int arg0) throws SQLException {
		LOG.info("getSchemaName");
		return this.resultSet.getConnection().getSchema();
	}

	public String getTableName(int arg0) throws SQLException {
		LOG.info("getTableName("+ arg0 + ")");
		ResultSet rs = resultSet.getConnection().getMetaData().getTables(null, null, null, null);
		for (int i = 1; i < arg0; i++) {
			rs.next();
		}
		LOG.info("return " + rs.getString("TABLE_NAME"));
		return rs.getString("TABLE_NAME");
	}

	public boolean isAutoIncrement(int arg0) throws SQLException {
		LOG.info("isAutoIncrement");
		return false;
	}

	public boolean isCaseSensitive(int arg0) throws SQLException {
		LOG.info("isCaseSensitive");
		return false;
	}

	public boolean isCurrency(int arg0) throws SQLException {
		LOG.info("isCurrency");
		return false;
	}

	public boolean isDefinitelyWritable(int arg0) throws SQLException {
		LOG.info("isDefinitelyWritable");
		return false;
	}

	public int isNullable(int arg0) throws SQLException {
		LOG.info("isNullable");
		return columnNullableUnknown;
	}

	public boolean isReadOnly(int arg0) throws SQLException {
		LOG.info("isReadOnly");
		return true;
	}

	public boolean isSearchable(int arg0) throws SQLException {
		LOG.info("isSearchable");
		return false;
	}

	public boolean isSigned(int arg0) throws SQLException {
		LOG.info("isSigned");
		return false;
	}

	public boolean isWritable(int arg0) throws SQLException {
		LOG.info("isWritable");
		return false;
	}

}
